package com.music.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.music.mapper.AlbumMapper;
import com.music.mapper.SingleSongMapper;
import com.music.pojo.Album;
import com.music.pojo.Playlist;
import com.music.pojo.SingleSong;
import com.music.service.AlbumService;
import com.music.service.SingleSongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AlbumServiceImpl implements AlbumService {
    @Autowired
    private AlbumMapper albumMapper;

    @Autowired
    private SingleSongMapper singleSongMapper;

    @Override
    public PageInfo<Album> findAlbumBySearch(String search, Integer pageNum) {
        PageHelper.startPage(pageNum,10);
        List<Album> albumBySearch = albumMapper.findAlbumBySearch(search);
        PageInfo<Album> pageInfo = new PageInfo<>(albumBySearch);
        return pageInfo;
    }

    @Override
    public Album findAlbumDetail(Integer alid,Integer uid) {
        Album album = albumMapper.findWithSongsAndSinger(alid);
        if (uid!=null){
            Integer anum = albumMapper.isFavorite(alid,uid);
            album.setAblumFavoriteStatus(anum);
            for (SingleSong song : album.getSongs()) {
                Integer num = singleSongMapper.isFavorite(song.getMid(),uid);
                song.setFavoriteStatus(num);
            }
        }
        return album;
    }

    @Override
    public void addFavoriteAlbum(Integer alid, Integer uid) {
        albumMapper.addFavoriteAlbum(alid,uid);
    }

    @Override
    public void removeFavoriteAlbum(Integer alid, Integer uid) {
        albumMapper.removeFavoriteAlbum(alid,uid);
    }

    @Override
    public Integer findCollectCount(Integer alid) {
       return albumMapper.findCollectCount(alid);
    }

    @Override
    public List<Album> findUserFollowSingerAlbum(Integer uid) {
        return albumMapper.findUserFollowSingerAlbum(uid);
    }
}
